package rabid;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import common.Constants;
import common.HistoWord;

public class TermList extends HashMap {
  private static final long serialVersionUID = 5897827078616417475L;
  private List termCounts;
  
  /**
   */
  public TermList(String termString, String fileName){
    termCounts = new ArrayList();
    put(fileName, new Integer(1));
    decodeTermString(termString);
  }
  
  private void decodeTermString(String termString) {
    String[] termCounts = termString.split(String.valueOf(Constants.TERM_SEPARATOR));
    for(int i = 0; i < termCounts.length; i += 2) {
      String word = termCounts[i];
      Integer count = new Integer(termCounts[i+1]);      
      put(word, count);
    }
  }
  
  public void put(String word, Integer count) {
    super.put(word, count);
    termCounts.add(new HistoWord(word, count));    
  }
  
  /**
   * Returns a List of HistoWord's
   */
  public List getTermCounts() {
    Collections.sort(termCounts);
    return termCounts;
  }

}
